/*
 * @Copyright: Citylife 2021
 * @Author: shaokui.gu
 * @Date: 2021-02-21 15:51:42
 * @Description: 
 */
import '../../base/base.dart';
import '../../base/base_page.dart';
import '../../components/table_view_cell.dart';
import '../../utils/locale/local_manager.dart';

class LanguageController extends TemplateController {
  final supportList = <LocaleItem>[].obs;

  final _currentLanguage = LocaleItem().obs;
  LocaleItem get currentLanguage => this._currentLanguage.value;
  set currentLanguage(value) => this._currentLanguage.value = value;

  @override
  void onInit() {
    super.onInit();
    currentLanguage = LocaleManager.instance.currentLanguage;
    supportList.addAll(LocaleManager.instance.supportLanguages);
  }

  changeLanguage(int index) {
    LocaleItem language = supportList[index];
    if (currentLanguage == language) return;
    currentLanguage = language;
    LocaleManager.instance.changeLanguage(language);
  }
}

class LanguagePage extends TemplatePage<LanguageController> {
  @override
  Widget buildBody(BuildContext context) {
    return Obx(() => ListView.builder(
          padding: EdgeInsets.zero,
          itemCount: controller.supportList.length,
          itemBuilder: (BuildContext context, int index) {
            String title = controller.supportList[index].name();
            return TableViewCell.defaultCell(
              title: title,
              height: 50.0.w,
              rightWidget: Obx(
                () => controller.currentLanguage.name() == title
                    ? Text(
                        "\uf058",
                        style: TextStyle(
                          fontSize: 18.0.sp,
                          fontFamily: IconFont,
                          color: theme.colors.defaultIconColor,
                        ),
                      )
                    : Container(),
              ),
              onTap: () {
                controller.changeLanguage(index);
              },
              // hasLine: (controller.darkModeEnabled && index != 5) ||
              // (!controller.darkModeEnabled && index != 2),
            );
          },
        ));
  }

  @override
  LanguageController initController() {
    return LanguageController();
  }
}
